<template>
  <el-row
    class="w-10 h-10 d-flex flex-column"
    v-if="$route.path === '/caption/funds'"
    style="over-flow: hidden;"
  >
    <el-col :span="24">
      <form class="el-form query-form el-form--label-right el-form--inline">
        <div class="el-form-item el-form-item--medium" v-if="admin.mType == 2 || admin.mType == 3">
          <label for="name" class="el-form-item__label" style="width: 60px;">区</label>
          <div class="el-form-item__content">
            <div class="el-input el-input--medium">
              <el-select v-model="query.mRegion" placeholder="惠济区" @change="get_townAtt()">
                <el-option label="惠济区" value="1349"></el-option>
              </el-select>
            </div>
            <!---->
          </div>
        </div>
        <div class="el-form-item el-form-item--medium" v-if="!(admin.mType >= 4)">
          <label for="date" class="el-form-item__label" style="width: 60px;">街道</label>
          <div class="el-form-item__content">
            <el-select
              v-model="query.mZhenid"
              clearable
              placeholder="请选择想要查看的街道"
              @change="get_villageAtt()"
            >
              <el-option
                v-for="(town, index) in townAtt"
                :key="index"
                :label="town.zName"
                :value="town.zhenId"
              ></el-option>
            </el-select>
          </div>
        </div>
        <div class="el-form-item el-form-item--medium" v-if="!(admin.mType == 5)">
          <label for="date" class="el-form-item__label" style="width: 60px;">村</label>
          <div class="el-form-item__content">
            <el-select v-model="query.mVillageid" clearable placeholder="请选择想要查看的村">
              <el-option
                v-for="(village, index) in villageAtt"
                :key="index"
                :label="village.vName"
                :value="village.villageId"
              ></el-option>
            </el-select>
          </div>
        </div>
        <div class="el-form-item el-form-item--medium">
          <label for="date" class="el-form-item__label" style="width: 80px;">资金类型</label>
          <div class="el-form-item__content">
            <el-select v-model="query.C_Type" clearable placeholder="请选择资金类型">
              <el-option v-for="item in cTypeAtt" :key="item" :label="item" :value="item"></el-option>
            </el-select>
          </div>
          <!---->
        </div>
        <div class="el-form-item el-form-item--medium ml-6">
          <!---->
          <div class="el-form-item__content">
            <el-button type="primary" icon="el-icon-search" @click="to_query">搜索</el-button>
          </div>
        </div>
        <div class="el-form-item el-form-item--medium ml-9" v-if="!(admin.mType == 3)">
          <div class="el-form-item__content">
            <el-button plain icon="el-icon-plus" @click="show_form">
              <span>添加</span>
            </el-button>
          </div>
          <!---->
        </div>
      </form>
    </el-col>
    <el-col :span="24">
      <el-table
        v-loading="loading"
        element-loading-text="拼命加载中"
        element-loading-spinner="el-icon-loading"
        element-loading-background="rgba(225, 225, 225, 0.8)"
        ref="multipleTable"
        :data="items"
        tooltip-effect="dark"
        style="width: 100%"
        border
        max-height="600"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="80" align="center"></el-table-column>
        <el-table-column type="index" width="80" align="center"></el-table-column>
        <el-table-column label="时间" width="220" align="center" prop="CD_Time" fixed></el-table-column>
        <el-table-column label="资金类型" width="240" align="center" prop="C_Type"></el-table-column>
        <el-table-column label="事由" width="240" align="center" prop="CD_XiangQing"></el-table-column>
        <el-table-column label="收入" width="120" align="center">
          <template slot-scope="scope">{{ scope.row.CD_Income ? scope.row.CD_Income + '元' : '' }}</template>
        </el-table-column>
        <el-table-column label="支出" width="120" align="center">
          <template
            slot-scope="scope"
          >{{ scope.row.CD_Expenditure ? scope.row.CD_Expenditure + '元' : '' }}</template>
        </el-table-column>
        <el-table-column label="所属村" width="120" align="center" prop="villName"></el-table-column>
        <el-table-column label="所属组" width="120" align="center" prop="groupName"></el-table-column>
        <el-table-column label="编辑" align="center" fixed="right" width="320">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-tickets" @click="showAll(scope.$index)">详情</el-button>
            <el-button
              v-if="!(admin.mType == 3)"
              type="warning"
              icon="el-icon-edit"
              @click="updata(scope.$index)"
            >编辑</el-button>
            <el-button
              v-if="!(admin.mType == 3)"
              type="danger"
              icon="el-icon-delete"
              @click="del(scope.$index, 'CD_XiangQing')"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div style="margin-top: 20px">
        <el-row class="d-flex jc-end">
          <el-col :span="12">
            <div
              class="grid-content bg-purple d-flex jc-between ai-center"
              v-if="!(admin.mType == 3)"
            >
              <el-button
                v-if="!(admin.mType == 3)"
                type="primary"
                icon="el-icon-delete"
                style="display: block;"
                class="h-1 w-1"
                @click="del_multip('CD_XiangQing')"
              ></el-button>
              <a
                target="_blank"
                href="http://www.huitengit.com/excelbiao/zijin.zip"
                class="ml-10 pl-10"
              >
                <el-button type="text" icon="el-icon-download">模板文件下载</el-button>
              </a>
              <el-select v-model="fileUrl" clearable placeholder="表类型">
                <el-option
                  v-for="item in [{name: '年资金表', url: 'caiwu/importYearCapitalExcel'},{name: '月资金表', url: 'caiwu/importCapitalExcel'}]"
                  :key="item.name"
                  :label="item.name"
                  :value="item.url"
                ></el-option>
              </el-select>
              <el-upload
                v-if="!(admin.mType == 3)"
                class="upload-demo"
                ref="upload"
                accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet, application/vnd.ms-excel"
                :action="$http.defaults.baseURL+fileUrl"
                :on-preview="handlePreview"
                :on-success="success"
                :auto-upload="false"
                :multiple="true"
                :data="{ managerId: admin.managerId }"
              >
                <el-button slot="trigger" size="small" type="primary">选取文件</el-button>
                <el-button
                  style="margin-left: 10px;"
                  size="small"
                  type="success"
                  @click="submitUpload"
                >上传到服务器</el-button>
                <div slot="tip" class="el-upload__tip">只能上传Excel文件，且不超过500kb</div>
              </el-upload>
            </div>
          </el-col>
          <el-col :span="12">
            <div class="grid-content bg-purple-light">
              <el-pagination
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="pagination.current_page"
                :page-sizes="pagination.page_sizes"
                :page-size="pagination.page_size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="pagination.total"
              ></el-pagination>
            </div>
          </el-col>
        </el-row>
      </div>
    </el-col>
    <el-dialog
      title="新增资金信息"
      v-if="$route.path === '/caption/funds'"
      width="40%"
      :show-close="false"
      :close-on-click-modal="false"
      :lock-scroll="true"
      :visible.sync="dialogFormVisible_create"
    >
      <el-form
        ref="createForm"
        :model="form"
        label-width="5.625rem"
        class="d-flex flex-column w-10"
      >
        <el-col :span="24">
          <el-tabs type="border-card">
            <el-tab-pane label="资金详情添加">
              <el-col :span="24" class="d-flex flex-column">
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">街道</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.zhenId"
                      :disabled="admin.mType >= 3"
                      clearable
                      placeholder="请选择想要查看的街道"
                      @change="get_villageAtt(true)"
                    >
                      <el-option
                        v-for="(town, index) in townAtt_form"
                        :key="index"
                        :label="town.zName"
                        :value="town.zhenId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">村名</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.villageId"
                      :disabled="admin.mType >= 5"
                      clearable
                      placeholder="请选择"
                      @change="get_groupAtt(true)"
                    >
                      <el-option
                        v-for="(village, index) in villageAtt_form"
                        :key="index"
                        :label="village.vName"
                        :value="village.villageId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">组名</label>
                  <div class="el-form-item__content">
                    <el-select v-model="form.cdZu" clearable placeholder="请选择">
                      <el-option
                        v-for="(group, index) in groupAtt"
                        :key="index"
                        :label="group.zName"
                        :value="group.zKey"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">资金类型</label>
                  <div class="el-form-item__content">
                    <el-select v-model="form.style" clearable placeholder="请选择资金类型">
                      <el-option v-for="item in cTypeAtt" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">时间</label>
                  <el-date-picker
                    v-model="form.cdTime"
                    clearable
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="选择年份"
                  ></el-date-picker>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">收入</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.cdIncome" clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">支出</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.cdExpenditure" clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">事由</label>
                  <div class="el-form-item__content flex-1 pr-2">
                    <el-input type="textarea" :rows="5" v-model="form.cdXiangqing"></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">图片上传</label>
                  <div class="el-form-item__content flex-1 pr-2">
                    <el-upload
                      ref="upload_file"
                      list-type="picture-card"
                      :on-success="success_c"
                      :on-preview="handlePictureCardPreview"
                      :on-remove="handleRemove"
                      :before-upload="set_form"
                      :on-change="file_change"
                      :auto-upload="false"
                      :data="form"
                      :multiple="true"
                      :http-request="uploadFile"
                    >
                      <i class="el-icon-plus"></i>
                    </el-upload>
                    <el-dialog :visible.sync="dialogVisible" size="tiny">
                      <img width="100%" v-if="form.src" :src="form.src" alt />
                    </el-dialog>
                  </div>
                </div>
              </el-col>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel_c">取 消</el-button>
        <el-button type="primary" @click="reset_c">重 置</el-button>
        <el-button type="primary" @click="create">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="资金信息编辑"
      v-if="$route.path === '/caption/funds'"
      width="40%"
      :show-close="false"
      :close-on-click-modal="false"
      :lock-scroll="true"
      :visible.sync="dialogFormVisible_edmit"
    >
      <el-form ref="form" :model="form" label-width="5.625rem" class="d-flex flex-column w-10">
        <el-col :span="24">
          <el-tabs type="border-card">
            <el-tab-pane label="资金详情编辑">
              <el-col :span="24" class="d-flex flex-column">
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">街道</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.zhenId"
                      clearable
                      :disabled="admin.mType >= 3"
                      placeholder="请选择想要查看的街道"
                      @change="get_villageAtt(true)"
                    >
                      <el-option
                        v-for="(town, index) in townAtt_form"
                        :key="index"
                        :label="town.zName"
                        :value="town.zhenId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">村名</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.C_Villageid"
                      clearable
                      :disabled="admin.mType >= 4"
                      placeholder="请选择"
                      @change="get_groupAtt(true)"
                    >
                      <el-option
                        v-for="(village, index) in villageAtt_form"
                        :key="index"
                        :label="village.vName"
                        :value="village.villageId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">组名</label>
                  <div class="el-form-item__content">
                    <el-select v-model="form.CD_zu" clearable placeholder="请选择">
                      <el-option
                        v-for="(group, index) in groupAtt"
                        :key="index"
                        :label="group.zName"
                        :value="group.zKey"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">时间</label>
                  <el-date-picker
                    v-model="form.CD_Time"
                    clearable
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="选择年份"
                  ></el-date-picker>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">收入</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.CD_Income" clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">支出</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.CD_Expenditure" clearable></el-input>
                  </div>
                </div>

                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">事由</label>
                  <div class="el-form-item__content flex-1 pr-2">
                    <el-input type="textarea" :rows="5" v-model="form.CD_XiangQing"></el-input>
                  </div>
                </div>

                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">图片上传</label>
                  <div class="el-form-item__content flex-1 pr-2">
                    <el-upload
                      ref="upload"
                      list-type="picture-card"
                      :on-success="success_c"
                      :on-preview="handlePictureCardPreview"
                      :on-remove="handleRemove"
                      :before-upload="set_form"
                      :on-change="file_change"
                      :auto-upload="false"
                      :file-list="form.src"
                      :data="form"
                      :multiple="true"
                      :http-request="uploadFile"
                    >
                      <i class="el-icon-plus"></i>
                    </el-upload>
                  </div>
                </div>
              </el-col>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel_u">取 消</el-button>
        <el-button type="primary" @click="reset_u">重 置</el-button>
        <el-button type="primary" @click="updata_save">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="资金信息详情"
      v-if="$route.path === '/caption/funds'"
      width="40%"
      :show-close="true"
      :close-on-click-modal="false"
      :lock-scroll="true"
      :visible.sync="dialogFormVisible_all"
    >
      <el-form ref="form" :model="form" label-width="5.625rem" class="d-flex flex-column w-10">
        <el-col :span="24">
          <el-tabs type="border-card">
            <el-tab-pane label="资金详情">
              <el-col :span="24" class="d-flex flex-column">
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">街道</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.zhenId"
                      clearable
                      disabled
                      placeholder="请选择想要查看的街道"
                      @change="get_villageAtt(true)"
                    >
                      <el-option
                        v-for="(town, index) in townAtt_form"
                        :key="index"
                        :label="town.zName"
                        :value="town.zhenId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">村名</label>
                  <div class="el-form-item__content">
                    <el-select
                      v-model="form.C_Villageid"
                      clearable
                      disabled
                      placeholder="请选择"
                      @change="get_groupAtt(true)"
                    >
                      <el-option
                        v-for="(village, index) in villageAtt_form"
                        :key="index"
                        :label="village.vName"
                        :value="village.villageId"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">组名</label>
                  <div class="el-form-item__content">
                    <el-select v-model="form.CD_zu" disabled clearable placeholder="请选择">
                      <el-option
                        v-for="(group, index) in groupAtt"
                        :key="index"
                        :label="group.zName"
                        :value="group.zKey"
                      ></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">时间</label>
                  <el-date-picker
                    disabled
                    v-model="form.CD_Time"
                    clearable
                    format="yyyy-MM-dd"
                    value-format="yyyy-MM-dd"
                    type="date"
                    placeholder="选择年份"
                  ></el-date-picker>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">资金类型</label>
                  <div class="el-form-item__content">
                    <el-select v-model="form.C_Type" disabled clearable placeholder="请选择资金类型">
                      <el-option v-for="item in cTypeAtt" :key="item" :label="item" :value="item"></el-option>
                    </el-select>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">收入</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.CD_Income" readonly="true" clearable></el-input>
                  </div>
                </div>
                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">支出</label>
                  <div class="el-form-item__content">
                    <el-input v-model="form.CD_Expenditure" readonly="true" clearable></el-input>
                  </div>
                </div>

                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">事由</label>
                  <div class="el-form-item__content flex-1 pr-2">
                    <el-input type="textarea" readonly="true" :rows="5" v-model="form.CD_XiangQing"></el-input>
                  </div>
                </div>

                <div class="el-form-item el-form-item--medium mb-2 w-10 d-flex jc-start">
                  <label for="date" class="el-form-item__label" style="width: 5.625rem;">图片上传</label>
                  <div class="el-form-item__content flex-1 pr-2">
                    <el-upload
                      ref="upload"
                      list-type="picture-card"
                      :on-success="success_c"
                      :on-preview="handlePictureCardPreview"
                      :on-remove="handleRemove"
                      :before-upload="set_form"
                      :on-change="file_change"
                      :auto-upload="false"
                      :file-list="form.src"
                      :data="form"
                      :multiple="true"
                      :http-request="uploadFile"
                      :disabled="true"
                    >
                      <i class="el-icon-plus"></i>
                    </el-upload>
                  </div>
                </div>
              </el-col>
            </el-tab-pane>
          </el-tabs>
        </el-col>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </el-row>
</template>

<script>
export default {
  data() {
    return {
      //登陆用户信息
      admin: {},
      pagination: {
        //当前页面
        current_page: 1,
        page_sizes: [20, 30, 50, 100],
        page_size: 20,
        //总共多少条
        total: 0,
        //总共多少页
        page_length: 0
      },
      items: [
        //表格内容
      ],
      init_items: [
        //原始表格渲染数据
      ],
      //删除的对象
      del_obj: {},
      delMany: {},
      //多删数组
      delAtt: [],
      multipleSelection: [],
      //模态框
      dialogFormVisible_create: false,
      dialogFormVisible_edmit: false,
      dialogFormVisible_all: false,

      loading: true,
      form: {
        //弹出框内容
      },
      //序列id
      index_id: 0,
      index: 0,

      query: {
        mVillageid: {},
        mRegion: "1349"
      },
      //搜索方式
      query_method: {},

      //镇村信息
      townAtt: [],
      townAtt_form: [],
      //对应村
      villageAtt: [],
      villageAtt_form: [],
      //对应组
      groupAtt: [],
      groupAtt_form: [],
      //村信息

      //类型选项
      cTypeAtt: ["现金收支", "银行收支"],
      //结果是否是搜索结果
      isQuery: false,
      //第一次请求搜索
      isFrist: true,
      //文件上传
      file: {},
      fileList: [],
      fileDate: {},
      fileUrl: "caiwu/importCapitalExcel",
      //搜索功能
      query_items: [],
      createUrl: "/caiwu/insertCapitaldetails"
    };
  },

  methods: {
    setQuery() {
      if (this.admin.mType == 4) {
        this.query.mRegion = this.admin.mRegion;
        this.query.mZhenid = this.admin.mZhenid;
        this.get_villageAtt();
      }
      if (this.admin.mType == 5) {
        this.query.mRegion = this.admin.mRegion;
        this.query.mZhenid = this.admin.mZhenid;
        this.query.mVillageid = this.admin.mVillageid;
      }
    },
    async get_townAtt(flag) {
      let res;
      if (flag) {
        res = await this.$http.get(`/caiwu/findzhenByRegionId/1349`);
        this.townAtt_form = res.data.data;
      } else {
        res = await this.$http.get(
          `/caiwu/findzhenByRegionId/${this.query.mRegion}`
        );
        this.townAtt = res.data.data;
      }
    },
    async get_villageAtt(flag) {
      let res;
      if (flag) {
        res = await this.$http.get(
          `/caiwu/findzhenByZhenId/${this.form.zhenId}`
        );
        this.villageAtt_form = res.data.data;
      } else {
        this.query.mVillageid = "";
        res = await this.$http.get(
          `/caiwu/findzhenByZhenId/${this.query.mZhenid}`
        );
        this.villageAtt = res.data.data;
      }
    },
    async get_groupAtt(flag) {
      let res;
      if (!this.form.villageId) {
        return;
      }
      if (flag) {
        res = await this.$http.get(`/caiwu/findZu/${this.form.villageId}`);
      }
      res = await this.$http.get(`/caiwu/findZu/${this.form.villageId}`);
      this.groupAtt = res.data.data;
    },
    //初始用户信息
    setModel(falg) {
      this.admin = this.$util.handleLocalStorage("get", "admin");
      let { mVillageid, mZhenid, mRegion, mType } = this.admin;
      this.model = { mVillageid, mZhenid, mRegion, mType };
      this.model.startPage = this.pagination.current_page;
      this.model.pageSize = this.pagination.page_size;
      if (falg) {
        this.query.startPage = this.pagination.current_page;
        this.query.pageSize = this.pagination.page_size;
      }
    },
    //得到数据
    async fetch(url, data) {
      this.setModel(data);
      let res;
      if (url && data) {
        res = await this.$http.post(url, this.Qs.stringify(data));
      } else {
        res = await this.$http.post(
          "/caiwu/findCapitaldetails",
          this.Qs.stringify(this.model)
        );
      }

      if (res.data.data) {
        Array.isArray(res.data.data) &&
          res.data.data.forEach(item => {
            item.villName = this.get_villName(item.C_Villageid);
            item.groupName = this.get_villName(item.C_Villageid);
            console.log(item.villName);
          });
        Array.isArray(res.data.data) &&
          res.data.data.forEach(item => {
            let att = [];
            if (item.files) {
              item.src = item.files.split(",");
              item.src.forEach(item => {
                if (item) {
                  att.push({
                    url: `${this.assetsURL}Capitaldetails/${item}`,
                    name: `${item}`
                  });
                }
              });
            }
            item.src = att;
          });
      }
      this.items = res.data.data;
      this.init_items = res.data;
      this.up_pagination();
      if (
        this.$util.getCookie("current_page") !==
        String(this.pagination.current_page)
      ) {
        this.handleCurrentChange(this.$util.getCookie("current_page"));
      }
      if (res.status == 200) {
        this.loading = false;
      }
      return res;
    },
    async get_villMes() {
      const res = await this.$http.get("/h/cacheVillage");
      this.villMes = res.data.data;
      console.log(this.villMes);
    },
    get_villName(id) {
      let res;
      if (this.villMes) {
        this.villMes.forEach(item => {
          if (item.villageId == id) {
            res = item.vName;
          }
        });
      }
      return res;
    },
    get_groupName(id) {
      let res;
      if (this.villMes) {
        this.villMes.forEach(item => {
          if (item.villageId == id) {
            res = item.vName;
          }
        });
      }
      return res;
    },
    //分页功能
    up_pagination() {
      this.pagination.page_length = Math.ceil(
        this.init_items.count / this.pagination.current_page
      );
      this.pagination.total = this.init_items.count;
    },
    async handleCurrentChange(val, flag) {
      this.pagination.current_page = val;
      this.$util.setCookie("current_page", this.pagination.current_page);
      if (this.isQuery) {
        this.loading = true;
        this.isFrist = false;
        this.to_query();
      } else {
        this.loading = true;
        const res = await this.fetch();
      }
    },
    //条数发生改变时
    handleSizeChange(val) {
      this.pagination.page_size = val;
      this.$util.setCookie("current_page", this.pagination.current_page);
      if (this.isQuery) {
        this.loading = true;
        this.to_query();
      } else {
        this.fetch();
      }
    },
    //删除预处理
    get_delObj() {
      let { managerId } = this.admin;
      this.del_obj = { managerId };
    },
    get_delMany() {
      this.delAtt.forEach(item => {
        item = String(item);
      });
      this.delMany.capitalDetailsId = this.delAtt;
    },
    //单条删除
    del(index, name) {
      this.get_delObj();
      this.index_id = index;
      this.form = this.items[index];
      this.del_obj.capitalDetailsId = this.form.CapitalDetails_Id;
      this.$confirm(
        `此操作将永久删除 ${this.items[index][name]}, 是否继续?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
          center: true
        }
      )
        .then(async index => {
          const res = await this.$http.delete(`/caiwu/deleteCapitaldetails`, {
            headers: { "Content-Type": "application/x-www-form-urlencoded" },
            data: this.Qs.stringify(this.del_obj)
          });
          console.log(".....////");
          console.log(res);
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.fetch();
          } else {
            this.$message({
              type: "error",
              message: "删除失败!"
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    //多条删除
    async del_multip(name) {
      if (!this.delAtt.length) {
        return;
      }
      this.get_delMany();
      let str = "";
      this.multipleSelection.forEach(item => {
        str += item[name] + "\n\t";
      });

      this.$confirm(`此操作将永久删除 ${str}, 是否继续?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
        center: true
      })
        .then(async index => {
          console.log(".....////");
          console.log(`.....////${this.delMany.capitalDetailsId}`);
          const res = await this.$http.delete(
            `/caiwu/deleteManyCapitaldetails/${this.delAtt.toString()}/${
              this.admin.managerId
            }`
          );
          console.log(".....////");
          console.log(res);
          if (res.status === 200) {
            this.$message({
              type: "success",
              message: "删除成功!"
            });
            this.fetch();
          } else {
            this.$message({
              type: "error",
              message: "删除失败!"
            });
          }
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    },
    initImage(imgAtt) {
      imgAtt.forEach(item => {
        if (item) {
          let file = {};
          this.appandFile(file, file => (item.file = file), item.url);
        }
      });
      this.fileList = imgAtt;
    },
    updata(index, flag) {
      this.form = JSON.parse(JSON.stringify(this.items[index]));
      this.initImage(this.form.src);
      this.index_id = index;
      this.show_init(true);
      if (flag) {
        this.dialogFormVisible_all = true;
      } else {
        this.dialogFormVisible_edmit = true;
      }
    },
    //序号选择发生变化时
    handleSelectionChange(val) {
      console.log(val);
      let att = [];
      this.multipleSelection = val;
      this.multipleSelection.forEach(item => {
        let { CapitalDetails_Id } = item;
        att.push(CapitalDetails_Id);
      });
      this.delAtt = att;
    },
    setIndex() {
      this.$basicMes.forEach((item, index) => {
        if (item.name == this.query.town) {
          this.villageAtt = this.$basicMes[index].village;
        }
      });
    },

    to_query() {
      if (this.isFrist) {
        this.handleCurrentChange(1, true);
      }
      this.fetch("/caiwu/findCapitaldetails", this.query);
      this.isQuery = true;
    },
    //请求数据
    //选择文件
    select_file() {},
    //文件上传
    submitUpload() {
      this.$refs.upload.submit();
    },
    handlePreview(file) {
      console.log(file);
    },
    success(res, file, files) {
      if (res.status === "成功") {
        this.$message({
          type: "success",
          message: "上传成功！"
        });
        this.fetch();
      } else {
        this.$message({
          type: "success",
          message: "上传失败!"
        });
      }
    },

    //create
    reset_c() {
      this.$refs["createForm"].resetFields();
      this.$confirm("确定重置新增数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info"
      }).then(() => {
        this.$message({
          type: "success",
          message: "已重置"
        });
      });
    },
    cancel_c() {
      this.$confirm("取消添加将不会保存该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "已取消！"
          });
          this.dialogFormVisible_create = false;
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    show_init(flag) {
      switch (this.admin.mType) {
        case 2:
          this.get_townAtt(true);
          if (flag) {
            this.form.zhenId = this.form.V_Zhen_xiang;
            this.get_villageAtt(true);
          }
          break;
        case 3:
          break;
        case 4:
          this.get_townAtt(true);
          this.form.zhenId = this.admin.mZhenid;
          this.get_villageAtt(true);
          break;
        case 5:
          this.get_townAtt(true);
          this.form.zhenId = this.admin.mZhenid;
          this.get_villageAtt(true);
          this.form.villageId = this.admin.mVillageid;
          this.get_groupAtt();
          break;
      }
    },
    show_form() {
      this.clearObj(this.form);
      this.show_init();
      this.dialogFormVisible_create = true;
    },
    handleRemove(file, fileList) {
      console.log(fileList, `-------------Remove`);
      this.fileList = fileList;
    },
    file_change(file, fileList) {
      this.fileList = fileList;
      console.log(fileList, `-------------change`);
    },
    uploadFile(files) {
      this.formDate.append("file", files.file);
    },
    async create() {
      this.form.managerId = this.admin.managerId;
      this.form.cVillageid = this.form.villageId;
      let res;
      if (this.fileList.length > 0) {
        this.formDate = new FormData();
        this.$refs.upload_file.submit();
        for (let item in this.form) {
          this.formDate.append(item, this.form[item]);
        }
        res = await this.$http.post(
          `/caiwu/insertCapitaldetails`,
          this.formDate
        );
        if (res.data.status === "success") {
          this.$message({
            type: "warning",
            message: `${res.data.data}`
          });
          this.fetch();
          this.dialogFormVisible_create = false;
        } else {
          this.$message({
            type: "success",
            message: "添加成功!"
          });
        }
      } else {
        res = await this.$http.post(
          `/caiwu/insertCapitaldetails`,
          this.Qs.stringify(this.form)
        );
        console.log(res);
        if (res.data.status === "success") {
          this.$message({
            type: "success",
            message: `${res.data.data}`
          });
          this.fetch();
          this.dialogFormVisible_create = false;
        } else {
          this.$message({
            type: "warning",
            message: `${res.data.data}`
          });
        }
      }
    },

    //edmit
    reset_u() {
      this.$confirm("确定重置编辑数据?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "info"
      }).then(() => {
        this.$message({
          type: "success",
          message: "已重置"
        });
        this.$refs["createForm"].resetFields();
      });
    },
    cancel_u() {
      this.$confirm("取消添加将不会保存该数据, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$message({
            type: "success",
            message: "已取消！"
          });

          this.dialogFormVisible_edmit = false;
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消操作"
          });
        });
    },
    async updata_save() {
      this.form.managerId = this.admin.managerId;
      let res;
      if (this.fileList.length > 0) {
        this.formDate = new FormData();
        this.fileList.forEach(file => {
          if (file.file) {
            this.formDate.append("file", file.file);
          } else {
            this.formDate.append("file", file.raw);
          }
        });
        for (let item in this.form) {
          if (item != "src" && item != "files" && this.form[item]) {
            this.formDate.append(item, this.form[item]);
          }
        }
        res = await this.$http.put(
          `/caiwu/updateCapitaldetails`,
          this.formDate
        );
        if (res.data.status === "success") {
          this.$message({
            type: "success",
            message: `${res.data.data}`
          });
          this.to_query();
          this.dialogFormVisible_edmit = false;
        } else {
          this.$message({
            type: "success",
            message: "添加成功!"
          });
        }
      } else {
        res = await this.$http.put(
          `/caiwu/updateCapitaldetails`,
          this.Qs.stringify(this.form)
        );
        console.log(res);
        if (res.data.status === "success") {
          this.$message({
            type: "success",
            message: `${res.data.data}`
          });
          this.to_query();
          this.dialogFormVisible_edmit = false;
        } else {
          this.$message({
            type: "warning",
            message: `${res.data.data}`
          });
        }
      }
    },
    cancel() {
      this.dialogFormVisible_all = false;
    },
    showAll(index) {
      this.updata(index, true);
    }
  },
  created() {
    this.admin = this.$util.handleLocalStorage("get", "admin");
    this.pagination.current_page = 1;
    this.$util.setCookie("current_page", this.pagination.current_page);
    this.get_townAtt();
    this.setQuery();
    this.get_villMes();
    this.fetch();
  }
};
</script>


